package database

import (
	"tech-service/config"
	"tech-service/internal/models"
	"tech-service/pkg/logbase"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func InitDataBase(config config.MysqlConfig, sqlLogger *logbase.MySQLLogger) (*gorm.DB, error) {

	db, err := gorm.Open(mysql.Open(config.DSN()), &gorm.Config{
		Logger: sqlLogger,
	})
	if err != nil {
		return nil, err
	}
	sqlDB, _ := db.DB()
	sqlDB.SetMaxIdleConns(10)
	sqlDB.SetMaxOpenConns(100)

	if err := db.AutoMigrate(&models.User{}); err != nil {
		return nil, err
	}

	return db, err
}
